﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Gift.DBUtility;//请先添加引用
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DB_Productclass
    /// </summary>
    public partial class DALProductclass
    {
        public DALProductclass()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("classid", "DB_Productclass");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int classid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from DB_Productclass");
            strSql.Append(" where classid=@classid");
            SqlParameter[] parameters = {
					new SqlParameter("@classid", SqlDbType.Int,4)
			};
            parameters[0].Value = classid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.DB_Productclass model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DB_Productclass(");
            strSql.Append("parentid,classname,classorder,classdepth,visible,classlink,amount,system,classdesc,classcontent,classnameen,classcontenten,classpic,classpcs,ishead,classprice,classkey,languages,shopId,BigClassId)");
            strSql.Append(" values (");
            strSql.Append("@parentid,@classname,@classorder,@classdepth,@visible,@classlink,@amount,@system,@classdesc,@classcontent,@classnameen,@classcontenten,@classpic,@classpcs,@ishead,@classprice,@classkey,@languages,@shopId,@BigClassId)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@parentid", SqlDbType.Int,4),
					new SqlParameter("@classname", SqlDbType.NVarChar,150),
					new SqlParameter("@classorder", SqlDbType.Int,4),
					new SqlParameter("@classdepth", SqlDbType.Int,4),
					new SqlParameter("@visible", SqlDbType.Bit,1),
					new SqlParameter("@classlink", SqlDbType.NVarChar,150),
					new SqlParameter("@amount", SqlDbType.Int,4),
					new SqlParameter("@system", SqlDbType.Bit,1),
					new SqlParameter("@classdesc", SqlDbType.NVarChar,250),
					new SqlParameter("@classcontent", SqlDbType.NVarChar,2000),
					new SqlParameter("@classnameen", SqlDbType.NVarChar,150),
					new SqlParameter("@classcontenten", SqlDbType.NVarChar,2000),
					new SqlParameter("@classpic", SqlDbType.NVarChar,150),
					new SqlParameter("@classpcs", SqlDbType.NVarChar,250),
					new SqlParameter("@ishead", SqlDbType.Bit,1),
					new SqlParameter("@classprice", SqlDbType.Int,4),
					new SqlParameter("@classkey", SqlDbType.NVarChar,250),
					new SqlParameter("@languages", SqlDbType.Int,4),
					new SqlParameter("@shopId", SqlDbType.Int,4),
					new SqlParameter("@BigClassId", SqlDbType.Int,4)};
            parameters[0].Value = model.parentid;
            parameters[1].Value = model.classname;
            parameters[2].Value = model.classorder;
            parameters[3].Value = model.classdepth;
            parameters[4].Value = model.visible;
            parameters[5].Value = model.classlink;
            parameters[6].Value = model.amount;
            parameters[7].Value = model.system;
            parameters[8].Value = model.classdesc;
            parameters[9].Value = model.classcontent;
            parameters[10].Value = model.classnameen;
            parameters[11].Value = model.classcontenten;
            parameters[12].Value = model.classpic;
            parameters[13].Value = model.classpcs;
            parameters[14].Value = model.ishead;
            parameters[15].Value = model.classprice;
            parameters[16].Value = model.classkey;
            parameters[17].Value = model.languages;
            parameters[18].Value = model.shopId;
            parameters[19].Value = model.BigClassId;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.DB_Productclass model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DB_Productclass set ");
            strSql.Append("parentid=@parentid,");
            strSql.Append("classname=@classname,");
            strSql.Append("classorder=@classorder,");
            strSql.Append("classdepth=@classdepth,");
            strSql.Append("visible=@visible,");
            strSql.Append("classlink=@classlink,");
            strSql.Append("amount=@amount,");
            strSql.Append("system=@system,");
            strSql.Append("classdesc=@classdesc,");
            strSql.Append("classcontent=@classcontent,");
            strSql.Append("classnameen=@classnameen,");
            strSql.Append("classcontenten=@classcontenten,");
            strSql.Append("classpic=@classpic,");
            strSql.Append("classpcs=@classpcs,");
            strSql.Append("ishead=@ishead,");
            strSql.Append("classprice=@classprice,");
            strSql.Append("classkey=@classkey,");
            strSql.Append("languages=@languages,");
            strSql.Append("shopId=@shopId,");
            strSql.Append("BigClassId=@BigClassId");
            strSql.Append(" where classid=@classid");
            SqlParameter[] parameters = {
					new SqlParameter("@parentid", SqlDbType.Int,4),
					new SqlParameter("@classname", SqlDbType.NVarChar,150),
					new SqlParameter("@classorder", SqlDbType.Int,4),
					new SqlParameter("@classdepth", SqlDbType.Int,4),
					new SqlParameter("@visible", SqlDbType.Bit,1),
					new SqlParameter("@classlink", SqlDbType.NVarChar,150),
					new SqlParameter("@amount", SqlDbType.Int,4),
					new SqlParameter("@system", SqlDbType.Bit,1),
					new SqlParameter("@classdesc", SqlDbType.NVarChar,250),
					new SqlParameter("@classcontent", SqlDbType.NVarChar,2000),
					new SqlParameter("@classnameen", SqlDbType.NVarChar,150),
					new SqlParameter("@classcontenten", SqlDbType.NVarChar,2000),
					new SqlParameter("@classpic", SqlDbType.NVarChar,150),
					new SqlParameter("@classpcs", SqlDbType.NVarChar,250),
					new SqlParameter("@ishead", SqlDbType.Bit,1),
					new SqlParameter("@classprice", SqlDbType.Int,4),
					new SqlParameter("@classkey", SqlDbType.NVarChar,250),
					new SqlParameter("@languages", SqlDbType.Int,4),
					new SqlParameter("@shopId", SqlDbType.Int,4),
					new SqlParameter("@BigClassId", SqlDbType.Int,4),
					new SqlParameter("@classid", SqlDbType.Int,4)};
            parameters[0].Value = model.parentid;
            parameters[1].Value = model.classname;
            parameters[2].Value = model.classorder;
            parameters[3].Value = model.classdepth;
            parameters[4].Value = model.visible;
            parameters[5].Value = model.classlink;
            parameters[6].Value = model.amount;
            parameters[7].Value = model.system;
            parameters[8].Value = model.classdesc;
            parameters[9].Value = model.classcontent;
            parameters[10].Value = model.classnameen;
            parameters[11].Value = model.classcontenten;
            parameters[12].Value = model.classpic;
            parameters[13].Value = model.classpcs;
            parameters[14].Value = model.ishead;
            parameters[15].Value = model.classprice;
            parameters[16].Value = model.classkey;
            parameters[17].Value = model.languages;
            parameters[18].Value = model.shopId;
            parameters[19].Value = model.BigClassId;
            parameters[20].Value = model.classid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int classid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DB_Productclass ");
            strSql.Append(" where classid=@classid");
            SqlParameter[] parameters = {
					new SqlParameter("@classid", SqlDbType.Int,4)
			};
            parameters[0].Value = classid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string classidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DB_Productclass ");
            strSql.Append(" where classid in (" + classidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.DB_Productclass GetModel(int classid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 classid,parentid,classname,classorder,classdepth,visible,classlink,amount,system,classdesc,classcontent,classnameen,classcontenten,classpic,classpcs,ishead,classprice,classkey,languages,shopId,BigClassId from DB_Productclass ");
            strSql.Append(" where classid=@classid");
            SqlParameter[] parameters = {
					new SqlParameter("@classid", SqlDbType.Int,4)
			};
            parameters[0].Value = classid;

            Gift.Model.DB_Productclass model = new Gift.Model.DB_Productclass();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["classid"] != null && ds.Tables[0].Rows[0]["classid"].ToString() != "")
                {
                    model.classid = int.Parse(ds.Tables[0].Rows[0]["classid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["parentid"] != null && ds.Tables[0].Rows[0]["parentid"].ToString() != "")
                {
                    model.parentid = int.Parse(ds.Tables[0].Rows[0]["parentid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["classname"] != null && ds.Tables[0].Rows[0]["classname"].ToString() != "")
                {
                    model.classname = ds.Tables[0].Rows[0]["classname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["classorder"] != null && ds.Tables[0].Rows[0]["classorder"].ToString() != "")
                {
                    model.classorder = int.Parse(ds.Tables[0].Rows[0]["classorder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["classdepth"] != null && ds.Tables[0].Rows[0]["classdepth"].ToString() != "")
                {
                    model.classdepth = int.Parse(ds.Tables[0].Rows[0]["classdepth"].ToString());
                }
                if (ds.Tables[0].Rows[0]["visible"] != null && ds.Tables[0].Rows[0]["visible"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["visible"].ToString() == "1") || (ds.Tables[0].Rows[0]["visible"].ToString().ToLower() == "true"))
                    {
                        model.visible = true;
                    }
                    else
                    {
                        model.visible = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["classlink"] != null && ds.Tables[0].Rows[0]["classlink"].ToString() != "")
                {
                    model.classlink = ds.Tables[0].Rows[0]["classlink"].ToString();
                }
                if (ds.Tables[0].Rows[0]["amount"] != null && ds.Tables[0].Rows[0]["amount"].ToString() != "")
                {
                    model.amount = int.Parse(ds.Tables[0].Rows[0]["amount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["system"] != null && ds.Tables[0].Rows[0]["system"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["system"].ToString() == "1") || (ds.Tables[0].Rows[0]["system"].ToString().ToLower() == "true"))
                    {
                        model.system = true;
                    }
                    else
                    {
                        model.system = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["classdesc"] != null && ds.Tables[0].Rows[0]["classdesc"].ToString() != "")
                {
                    model.classdesc = ds.Tables[0].Rows[0]["classdesc"].ToString();
                }
                if (ds.Tables[0].Rows[0]["classcontent"] != null && ds.Tables[0].Rows[0]["classcontent"].ToString() != "")
                {
                    model.classcontent = ds.Tables[0].Rows[0]["classcontent"].ToString();
                }
                if (ds.Tables[0].Rows[0]["classnameen"] != null && ds.Tables[0].Rows[0]["classnameen"].ToString() != "")
                {
                    model.classnameen = ds.Tables[0].Rows[0]["classnameen"].ToString();
                }
                if (ds.Tables[0].Rows[0]["classcontenten"] != null && ds.Tables[0].Rows[0]["classcontenten"].ToString() != "")
                {
                    model.classcontenten = ds.Tables[0].Rows[0]["classcontenten"].ToString();
                }
                if (ds.Tables[0].Rows[0]["classpic"] != null && ds.Tables[0].Rows[0]["classpic"].ToString() != "")
                {
                    model.classpic = ds.Tables[0].Rows[0]["classpic"].ToString();
                }
                if (ds.Tables[0].Rows[0]["classpcs"] != null && ds.Tables[0].Rows[0]["classpcs"].ToString() != "")
                {
                    model.classpcs = ds.Tables[0].Rows[0]["classpcs"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ishead"] != null && ds.Tables[0].Rows[0]["ishead"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["ishead"].ToString() == "1") || (ds.Tables[0].Rows[0]["ishead"].ToString().ToLower() == "true"))
                    {
                        model.ishead = true;
                    }
                    else
                    {
                        model.ishead = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["classprice"] != null && ds.Tables[0].Rows[0]["classprice"].ToString() != "")
                {
                    model.classprice = int.Parse(ds.Tables[0].Rows[0]["classprice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["classkey"] != null && ds.Tables[0].Rows[0]["classkey"].ToString() != "")
                {
                    model.classkey = ds.Tables[0].Rows[0]["classkey"].ToString();
                }
                if (ds.Tables[0].Rows[0]["languages"] != null && ds.Tables[0].Rows[0]["languages"].ToString() != "")
                {
                    model.languages = int.Parse(ds.Tables[0].Rows[0]["languages"].ToString());
                }
                if (ds.Tables[0].Rows[0]["shopId"] != null && ds.Tables[0].Rows[0]["shopId"].ToString() != "")
                {
                    model.shopId = int.Parse(ds.Tables[0].Rows[0]["shopId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BigClassId"] != null && ds.Tables[0].Rows[0]["BigClassId"].ToString() != "")
                {
                    model.BigClassId = int.Parse(ds.Tables[0].Rows[0]["BigClassId"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select classid,parentid,classname,classorder,classdepth,visible,classlink,amount,system,classdesc,classcontent,classnameen,classcontenten,classpic,classpcs,ishead,classprice,classkey,languages,shopId,BigClassId ");
            strSql.Append(" FROM DB_Productclass ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetMainMenuList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select classid,parentid,classname,classorder,classdepth,visible,classlink,system,classdesc,languages,shopId ");
            strSql.Append(" FROM DB_Productclass ");
            strSql.Append(" where classdepth = 1 and visible = 1 order by classorder asc ");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }

            return null;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetHeadMenuLevel2List(int iClassId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select classid,parentid,classname,classorder,classdepth,visible,classlink,system,classdesc,languages,shopId ");
            strSql.Append(" FROM DB_Productclass ");
            strSql.Append(" where classdepth = 2 and visible = 1 and parentId = @ClassId order by classorder asc ");

            SqlParameter[] parameters = {
                    new SqlParameter("@ClassId", SqlDbType.Int,4)
                    };
            parameters[0].Value = iClassId;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(),parameters);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }

            return null;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetHeadMenuLevel3List(int iParentId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select classid,parentid,classname,classorder,classdepth,visible,classlink,system,classdesc,languages,shopId ");
            strSql.Append(" FROM DB_Productclass ");
            strSql.Append(" where classdepth = 3 and visible = 1 and parentId = @ParentId order by classorder asc ");

            SqlParameter[] parameters = {
                    new SqlParameter("@ParentId", SqlDbType.Int,4)
                    };
            parameters[0].Value = iParentId;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(),parameters);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }

            return null;
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" classid,parentid,classname,classorder,classdepth,visible,classlink,amount,system,classdesc,classcontent,classnameen,classcontenten,classpic,classpcs,ishead,classprice,classkey,languages,shopId,BigClassId ");
            strSql.Append(" FROM DB_Productclass ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM DB_Productclass ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.classid desc");
            }
            strSql.Append(")AS Row, T.*  from DB_Productclass T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "DB_Productclass";
            parameters[1].Value = "classid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

